package com.rocksea.app;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.ButterKnife;

/**
 * Created by 吴志华 on 2017/7/20.
 * Fragment基类
 */

public abstract class BaseFragment extends Fragment{

    private View mContent;
    private View mProgressBarView;
    private View mErrorView;

    protected View mRootView;
    public TextView mErrorText;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        mRootView = inflater.inflate(getContentViewId(),container,false);
        ButterKnife.inject(this,mRootView);
        initLayout();
        initView();
        initData();
        return mRootView;
    }
    protected abstract void initView();
    protected abstract void initData();
    protected abstract void requestAgain();
    protected abstract int getContentViewId();
    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    public int getStateBarHeight(Activity a) {
        int result = 0;
        int resourceId = a.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = a.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    private void initLayout()
    {
        mContent = mRootView.findViewById(R.id.Main_Content);
        mErrorView = mRootView.findViewById(R.id.Error_Layout);
        mProgressBarView = mRootView.findViewById(R.id.ProgressBar);
        mErrorText = (TextView) mRootView.findViewById(R.id.Error_Text);
        mErrorText.setOnClickListener(onClickListener);
    }

    public void loading()
    {
        mContent.setVisibility(View.GONE);
        mProgressBarView.setVisibility(View.VISIBLE);
        mErrorView.setVisibility(View.GONE);
    }

    public void loadCompleted()
    {
        mContent.setVisibility(View.VISIBLE);
        mProgressBarView.setVisibility(View.GONE);
        mErrorView.setVisibility(View.GONE);
    }

    public void loadFailed()
    {
        mErrorText.setText("加载失败");
        mErrorView.setVisibility(View.VISIBLE);
        mProgressBarView.setVisibility(View.GONE);
        mContent.setVisibility(View.GONE);
    }



    private TextView.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            requestAgain();
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
